<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js" type="text/javascript"></script>
  <style>
    .list-item {
      display: flex;
      justify-content: space-between
    }

    .gray {
      background-color: white;
    }

    .checked {
      background-color: gray
    }

  </style>
</head>

<body>
  <div id='root'>
    <div>
      <input type="text" v-model='value' @keyup.13='addTask'><button @click='addTask'>add</button>
    </div>

    <div>
      <div v-for='(item,index) in list' :class='["list-item", { checked:item.checked } , {gray: index%2==0}]'
        @click='check(index)' :key='item.key'>
        <div>{{ item.name }} </div>
        <div @click.stop='remove(index)'>x </div>
      </div>
    </div>
  </div>

  <script>
    let vm = new Vue({
      el: '#root',
      data: function () {
        return {
          value: '',
          list: [{
              name: '任务一',
              checked: false,
              key: Math.random()
            },
            {
              name: '任务二',
              checked: false,
              key: Math.random()
            },
            {
              name: '任务三',
              checked: false,
              key: Math.random()
            }
          ] // 任务列表
        }
      },
      methods: {
        addTask() {
          let value = this.value;
          if (value) {
            this.list.push({
              name: this.value,
              checked: false,
              key: Math.random()
            })
            this.value = '';
          }
        },
        check(index) {
          console.log('checked');
          this.list[index].checked = !this.list[index].checked;
        },
        remove(index) {
          this.list.splice(index, 1)
        }
      },
      computed: {

      }
    })

  </script>
</body>

</html>
